package com.youcan.controller.modelcontroller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.youcan.entity.Sidedishes;
import com.youcan.service.modelservices.SidedishesService;
import com.youcan.util.Constants;

/**
 * @author time E-mail:timezone85@gmail.com
 * @date:2012-8-19 上午11:07:06
 * @version :V1.0
 *
 */

@Component
@Controller
public class SidedishesController {
	protected final Log logger = LogFactory.getLog(SidedishesController.class);

	@Autowired
	@Qualifier("sidedishesServiceImpl")
	private SidedishesService sidedishesService;

	//通过注解进行转发控制 POST来的请求并把参数绑定
	@ResponseBody
	@RequestMapping(value = "/AddSidedishes", method = RequestMethod.POST)
	public JSONObject AddSidedishes(
			@RequestParam("sideName") String sideName,
			@RequestParam("price") Double price,
			@RequestParam("img") String img,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, JSONException {
		
		Sidedishes sidedishes = new Sidedishes();
		JSONObject json = new JSONObject();
		try {
			sidedishes.setSideName(sideName);
			sidedishes.setPrice(price);
			sidedishes.setImg(img);
			
			sidedishesService.addEntity(sidedishes);
			json.put(Constants.RES_CODE, Constants.CODE_SUCCESS);
			json.put(Constants.RES_MESSAGE, "配菜添加成功。");
		} catch (Exception e) {
			json.put(Constants.RES_CODE, Constants.CODE_FAILED);
			json.put(Constants.RES_MESSAGE, "配菜添加失败。");
		}

		return json;
	}

}
